<?php

class Karakterark_model extends CI_Model {

	function slet_stats_over_level($entitet_id,$level){
		$query = $this->db->query('
			DELETE from karakterark_stats
			WHERE id_entitet = '.$entitet_id.'
			AND level >= '.$level.'
		');
	}

	function alle_stats($entitet_id){
		$element = new individ();
		$element->find_level($entitet_id);
		$level = $element->level;
		$taeller = $level;
		$stats = array();
		while($taeller > -1){
			$stats[$taeller]['pot'] = $this->karakterark_model->stats($entitet_id, "pot", $taeller);
			$stats[$taeller]['temp'] = $this->karakterark_model->stats($entitet_id, "temp", $taeller);
			$taeller = $taeller - 1;
		}
		return $stats;
	}

	function stats($entitet_id, $type, $level) //$type="temp"/"pot"
	{
		
			//echo "<br>(model>rolemaster/karakterark_model.php) level er "; echo $level;
		$query = $this->db->query("
			SELECT * 
			FROM karakterark_stats
			WHERE id_entitet = ".$entitet_id."
			AND type ='".$type."'
			AND level = ".$level."
			");
	
		$stats = array();
		
		if($query->num_rows() > 0){
			$row = $query->row();
			
			$stats = array(
				'Lu' => $row->Lu,	
				'Co' => $row->Co,
				'Ag' => $row->Ag,
				'Re' => $row->Re,
				'Me' => $row->Me,
				'SD' => $row->SD,
				'St' => $row->St,
				'Qu' => $row->Qu,
				'Em' => $row->Em,
				'Pr' => $row->Pr,
				'In' => $row->In,
			);
		}else{
			$stats = 0;
		}
	

	
		return $stats;
		
	}
	
	function rediger_stats($entitet_id, $type, $level, $ny_Lu, $ny_Co, $ny_Ag, $ny_Re, $ny_Me, $ny_SD, $ny_St, $ny_Qu, $ny_Pr, $ny_Em, $ny_In)
	{

			$nye_stats = 
					array(
						'id_entitet' => $entitet_id,
						'type' => $type,
						'level' => $level,
						'Lu' => $ny_Lu,	
						'Co' => $ny_Co,
						'Ag' => $ny_Ag,
						'Re' => $ny_Re,
						'Me' => $ny_Me,
						'SD' => $ny_SD,
						'St' => $ny_St,
						'Qu' => $ny_Qu,
						'Em' => $ny_Em,
						'Pr' => $ny_Pr,
						'In' => $ny_In,
				);
				
			print_r($nye_stats);

				$query =
				"
					SELECT COUNT(*) as antal
					FROM karakterark_stats
					WHERE id_entitet = ".$entitet_id."
					AND type ='".$type."'
					AND level = ".$level."
				";
				$result = mysql_query($query) or die(mysql_error());
				$row = mysql_fetch_array($result) or die(mysql_error());
		
				if ($row['antal'] == 0) 
				{
					$this->db->insert('karakterark_stats', $nye_stats);
				}
				else
				{
			
					$this->db->where('id_entitet', $entitet_id);
					$this->db->where('type', $type);
					$this->db->where('level', $level);
					$this->db->update('karakterark_stats', $nye_stats);
				}
				



	}

	function stat_bonus_distribution($stat, $giv_mig){
		if($stat > 100){ $stat = 100; }
		if($stat < 1){ $stat = 1; }
		$query = $this->db->query('
			SELECT '.$giv_mig.'
			FROM stat_bonus_distribution_table
			WHERE stat = '.$stat.'
		');
		$row = $query->row();
		return $row->$giv_mig;
	}


} 



?>
